using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace GEM.Infrastructure.Extensions;

public static class CorsExtensions
{
    public const string DefaultCorsPolicyName = "GemCorsPolicy";

    public static IServiceCollection AddGemCors(this IServiceCollection services, IConfiguration configuration)
    {
        var corsOrigins = configuration.GetSection("Cors:Origins").Get<string[]>() ??
            new[] { "http://localhost:5173" };

        services.AddCors(options =>
        {
            options.AddPolicy(DefaultCorsPolicyName, builder =>
            {
                builder
                    .WithOrigins(corsOrigins)
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials()
                    .WithExposedHeaders("Content-Disposition");
            });
        });

        return services;
    }
}